# coding: utf-8
from sqlalchemy import BigInteger, Column, DateTime, ForeignKey, Index, Integer, Numeric, String, Table, Text
from sqlalchemy.schema import FetchedValue
from sqlalchemy.orm import relationship
from flask_sqlalchemy import SQLAlchemy


db = SQLAlchemy()


class AppAccessLog(db.Model):
    __tablename__ = 'app_access_log'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    uid = db.Column(db.BigInteger, nullable=False, index=True, server_default=db.FetchedValue())
    referer_url = db.Column(db.String(255), nullable=False, server_default=db.FetchedValue())
    target_url = db.Column(db.String(255), nullable=False, server_default=db.FetchedValue())
    query_params = db.Column(db.Text, nullable=False)
    ua = db.Column(db.String(255), nullable=False, server_default=db.FetchedValue())
    ip = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue())
    note = db.Column(db.String(1000), nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())


class AppErrorLog(db.Model):
    __tablename__ = 'app_error_log'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    referer_url = db.Column(db.String(255), nullable=False, server_default=db.FetchedValue())
    target_url = db.Column(db.String(255), nullable=False, server_default=db.FetchedValue())
    query_params = db.Column(db.Text, nullable=False)
    content = db.Column(db.String, nullable=False)
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())


class Customer(db.Model):
    __tablename__ = 'customer'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    nickname = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue())
    mobile = db.Column(db.String(11), nullable=False, server_default=db.FetchedValue())
    sex = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    avatar = db.Column(db.String(200), nullable=False, server_default=db.FetchedValue())
    salt = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue())
    reg_ip = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue())
    status = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())


class CustomerCoupon(db.Model):
    __tablename__ = 'customer_coupon'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    c_id = db.Column(db.ForeignKey('food_db.customer.id'), nullable=False, index=True)
    sc_id = db.Column(db.ForeignKey('food_db.shop_coupon.id'), nullable=False, index=True)
    is_used = db.Column(db.Integer, nullable=False)
    is_valid = db.Column(db.Integer, nullable=False)
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())

    c = db.relationship('Customer', primaryjoin='CustomerCoupon.c_id == Customer.id', backref='customer_coupons')
    sc = db.relationship('ShopCoupon', primaryjoin='CustomerCoupon.sc_id == ShopCoupon.id', backref='customer_coupons')


class Food(db.Model):
    __tablename__ = 'food'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue())
    original_price = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue())
    main_image = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue())
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    is_recommend = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    is_discount = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    discount = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    present_price = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue())
    shop_id = db.Column(db.ForeignKey('food_db.shop.id'), nullable=False, index=True, server_default=db.FetchedValue())

    shop = db.relationship('Shop', primaryjoin='Food.shop_id == Shop.id', backref='foods')
    fts = db.relationship('FoodType', secondary='food_db.food_link_ftype', backref='foods')


t_food_link_ftype = db.Table(
    'food_link_ftype',
    db.Column('f_id', db.ForeignKey('food_db.food.id'), primary_key=True, nullable=False, index=True),
    db.Column('ft_id', db.ForeignKey('food_db.food_type.id'), primary_key=True, nullable=False, index=True),
    schema='food_db'
)


class FoodType(db.Model):
    __tablename__ = 'food_type'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue())
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())


class OauthCustomerBind(db.Model):
    __tablename__ = 'oauth_customer_bind'
    __table_args__ = (
        db.Index('idx_type_openid', 'type', 'open_id'),
        {'schema': 'food_db'}
    )

    id = db.Column(db.Integer, primary_key=True)
    c_id = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    c_type = db.Column(db.String(20), nullable=False, server_default=db.FetchedValue())
    type = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    open_id = db.Column(db.String(80), nullable=False, server_default=db.FetchedValue())
    union_id = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue())
    extra = db.Column(db.Text, nullable=False)
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())


class Order(db.Model):
    __tablename__ = 'order'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    order_sn = db.Column(db.String(40), nullable=False, unique=True, server_default=db.FetchedValue())
    c_id = db.Column(db.ForeignKey('food_db.customer.id'), nullable=False, index=True)
    total_price = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue())
    note = db.Column(db.Text, nullable=False)
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    shop_id = db.Column(db.ForeignKey('food_db.shop.id'), nullable=False, index=True)
    status = db.Column(db.Integer, nullable=False)
    meal_number = db.Column(db.String(10), nullable=False)
    table_id = db.Column(db.ForeignKey('food_db.table_item.id'), nullable=False, index=True)
    minus_cash = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue())

    c = db.relationship('Customer', primaryjoin='Order.c_id == Customer.id', backref='orders')
    shop = db.relationship('Shop', primaryjoin='Order.shop_id == Shop.id', backref='orders')
    table = db.relationship('TableItem', primaryjoin='Order.table_id == TableItem.id', backref='orders')


class OrderItem(db.Model):
    __tablename__ = 'order_item'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    order_id = db.Column(db.ForeignKey('food_db.order.id'), nullable=False, index=True)
    count = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    price = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue())
    food_id = db.Column(db.ForeignKey('food_db.food.id'), nullable=False, index=True)
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())

    food = db.relationship('Food', primaryjoin='OrderItem.food_id == Food.id', backref='order_items')
    order = db.relationship('Order', primaryjoin='OrderItem.order_id == Order.id', backref='order_items')


class Shop(db.Model):
    __tablename__ = 'shop'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), nullable=False)
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())


class ShopCoupon(db.Model):
    __tablename__ = 'shop_coupon'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False, server_default=db.FetchedValue())
    start_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    end_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    time_type = db.Column(db.Integer, nullable=False)
    full_cash = db.Column(db.Numeric(10, 2), nullable=False)
    minus_cash = db.Column(db.Numeric(10, 2), nullable=False)
    cash_type = db.Column(db.Integer, nullable=False)
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    shop_id = db.Column(db.ForeignKey('food_db.shop.id'), nullable=False, index=True)

    shop = db.relationship('Shop', primaryjoin='ShopCoupon.shop_id == Shop.id', backref='shop_coupons')


class TableItem(db.Model):
    __tablename__ = 'table_item'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(40), nullable=False, server_default=db.FetchedValue())
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    shop_id = db.Column(db.ForeignKey('food_db.shop.id'), nullable=False, index=True)

    shop = db.relationship('Shop', primaryjoin='TableItem.shop_id == Shop.id', backref='table_items')


class User(db.Model):
    __tablename__ = 'user'
    __table_args__ = {'schema': 'food_db'}

    uid = db.Column(db.BigInteger, primary_key=True)
    nickname = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue())
    mobile = db.Column(db.String(20), nullable=False, server_default=db.FetchedValue())
    email = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue())
    sex = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    avatar = db.Column(db.String(64), nullable=False, server_default=db.FetchedValue())
    login_name = db.Column(db.String(20), nullable=False, unique=True, server_default=db.FetchedValue())
    login_pwd = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue())
    login_salt = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue())
    status = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    shop_id = db.Column(db.Integer, nullable=False)
